﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Sales_SaleDitails : ATPages
{
    Function f = new Function();
    protected void Page_Load(object sender, EventArgs e)
    {
        lblPath.Text = f.getModuleidAdd(Request.QueryString["moduid"].ToString());
        hfxs.Value = Request.QueryString["moduid"].ToString();
        if (!IsPostBack)
        {
            HidWareid.Value=getWareId();
            initKdate();
            initData();
            inDepartment();
        }
    }

    
    private void inDepartment()
    {
        ZSql Dep = new ZSql();
        Dep.Open("select * from Department");
        ddrDepartment.DataSource = Dep.m_table;
        ddrDepartment.DataTextField = "Deptname";
        ddrDepartment.DataValueField = "Deptid";
        ddrDepartment.DataBind();
        ddrDepartment.Items.Insert(0, new ListItem("所有部门", ""));
        Dep.Close();
        txtKdate.Text = Request.QueryString["Kdate"];
        txtJdate.Text = Request.QueryString["Jdate"];
        modul.Text = Request.QueryString["modul"];
        GuestName.Text = Request.QueryString["GuestName"];
        ComodBarcode.Text = Request.QueryString["ComodBarcode"];
        ddltypeSale.SelectedValue = Request.QueryString["typeSale"];
        ddrDepartment.SelectedValue = Request.QueryString["deptid"];
        ComoditiesName.Text = Request.QueryString["comodName"];
    }

    private void initData()
    {   
        string search="";
        string sql = "select * from (select a.*,(select top 1 realname from UserInfo where a.userid=userid) as realname, (select GuestName from CS_Guest where a.guestid=guestid) as GuestName,(select StaffName from CS_Staff where StaffId=a.Saleman) as  StaffName,(select deptName from department where a.deptid=deptid) as deptName  from CS_Sales a )TblSales where 1=1 and isDel=0";

        if (!string.IsNullOrEmpty(Request.QueryString["Kdate"]) && !string.IsNullOrEmpty(Request.QueryString["Jdate"]))//时间起止
        {
            search += "&Kdate=" + Request.QueryString["Kdate"].Trim();
            search += "&Jdate=" + Request.QueryString["Jdate"].Trim();
            sql += " and c_date>='" + Request.QueryString["Kdate"] + "' and c_date<='" + Request.QueryString["Jdate"] + "' ";
        }
        if (!string.IsNullOrEmpty(Request.QueryString["modul"]))//订单号
        {
            search += "&modul=" + Request.QueryString["modul"].Trim();
            sql += " and modul like '%" + Request.QueryString["modul"].Trim() + "%'";
        }
        if (!string.IsNullOrEmpty(Request.QueryString["GuestName"]))//客户名称
        {
            search += "&GuestName=" + Request.QueryString["GuestName"].Trim();
            sql += " and GuestName like '%" + Request.QueryString["GuestName"].Trim()+"%'";
        }
        if (!string.IsNullOrEmpty(Request.QueryString["comodName"]))//商品名称
        {
            search += "&comodName=" + Request.QueryString["comodName"].Trim();
            sql += " and comodName like '%" + Request.QueryString["comodName"].Trim()+"%'";
        }
        if (!string.IsNullOrEmpty(Request.QueryString["ComodBarcode"]))//型号编号
        {
            search += "&ComodBarcode=" + Request.QueryString["ComodBarcode"].Trim();
            sql += " and ComodBarcode='" + Request.QueryString["ComodBarcode"].Trim()+"'";
        }
        if (!string.IsNullOrEmpty(Request.QueryString["typeSale"]))//销售类型
        {
            search += "&typeSale=" + Request.QueryString["typeSale"].Trim();
            sql += " and typeSale=" + Request.QueryString["typeSale"].Trim();
        }
        if (!string.IsNullOrEmpty(Request.QueryString["deptid"]))//部门
        {
            search += "&deptid=" + Request.QueryString["deptid"].Trim();
            sql += " and deptid=" + Request.QueryString["deptid"].Trim();
        }
        sql += " order by SalesId desc";

        PageNavigator1.Url = "../sales/saleDitails.aspx?moduid=" + hfxs.Value + search;
        PageNavigator1.Total=1;
        PageNavigator1.Query = sql;
      
        
    }
    private string getWareId()
    {
        ZSql zs = new ZSql();
        string sql = "select WarehouseId from UserInfo where userid=" + Request.Cookies["user"].Values["id"];
        zs.Open(sql);

        zs.Close();
        return zs.m_table.Rows[0]["WarehouseId"].ToString();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string search = "";
        if (!string.IsNullOrEmpty(txtKdate.Text) && !string.IsNullOrEmpty(txtJdate.Text.Trim()))//时间起止
        {
            search += "&Kdate=" + txtKdate.Text.Trim();
            search += "&Jdate=" + txtJdate.Text.Trim();
        }
        if (!string.IsNullOrEmpty(modul.Text.Trim()))//订单号
        {
            search += "&modul=" + Common.FormatParameter(modul.Text.Trim());
        }
        if (!string.IsNullOrEmpty(GuestName.Text.Trim()))//客户名称
        {
            search += "&GuestName=" + Common.FormatParameter(GuestName.Text).Trim();
        }
        if (!string.IsNullOrEmpty(ComoditiesName.Text.Trim()))//商品名称
        {
            search += "&comodName=" + Common.FormatParameter(ComoditiesName.Text).Trim();
        }
        if (!string.IsNullOrEmpty(ComodBarcode.Text))//型号编号
        {
            search += "&ComodBarcode=" + Common.FormatParameter(ComodBarcode.Text.Trim());
        }
        if (!string.IsNullOrEmpty(ddltypeSale.SelectedValue))//销售类型
        {
            search += "&typeSale=" + Common.FormatParameter(ddltypeSale.SelectedValue.Trim());
        }

        if (!string.IsNullOrEmpty(ddrDepartment.SelectedValue))//部门
        {
            search += "&deptid=" + Common.FormatParameter(ddrDepartment.SelectedValue.Trim());
        }

        PageNavigator1.Url = "../sales/saleDitails.aspx?moduid=" + hfxs.Value + search;
        Response.Redirect("saleDitails.aspx?moduid=" + hfxs.Value + search);
    }


    protected void GvData_RowDataBound(object sender, GridViewRowEventArgs e)//在邦定数据
    {
        //f.checkLevels(Request.Cookies["user"].Values["roleid"], hfxs.Value, 4, GvDataType, btndel, btnallflag, btnAdd);//权限
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int typeSale = Convert.ToInt16(DataBinder.Eval(e.Row.DataItem, "typeSale"));
            if (typeSale == 1)
            {
                e.Row.Cells[12].Text = "零售";
            }
            else if (typeSale == 2)
            {
                e.Row.Cells[12].Text = "批发";
            }
            else if (typeSale == 3)
            {
                e.Row.Cells[12].Text = "工程";
            }
            else 
            {
                e.Row.Cells[12].Text = "未知";
            }
        }
    }


    private void initKdate()
    {
        txtJdate.Text = DateTime.Now.Date.ToString("yyyy-MM-dd");//当前日期
        txtKdate.Text = DateTime.Now.Date.ToString("yyyy-MM") + "-01";//当月的第一天
    }
}
